import fs from "fs";
import fsp from "fs/promises";
import path from "path";

const ContentMap = new Map<string, string>()
const mdTree = [];
const contentPath = `src/content`

function eachFound(path: string, content: string) {
  ContentMap.set(path, content)
}

function createTree(name: string, isfolder: boolean) {
  //生成索引树
  if (isfolder) {
    mdTree.push(`folder:${name}`);
  } else {
    mdTree.push(name);
  }
}

async function createJson(cPath: string) {
  //记得要json格式
  const data = await fsp.readFile(`${cPath}`, "utf-8");
  eachFound(cPath, data);
}

async function rc(cPath: string, lay = 0) {
  const files = await fsp.readdir(cPath /*content的位置*/);
  await Promise.all(files.map(async (fName) => {
    if (fName.split("/").some((s) => s[0] === "_")) return
    else if (fs.lstatSync(path.join(cPath, fName)).isDirectory()) {
      createTree(fName, true);
      await rc(path.join(cPath, fName), lay + 1);
    } else if (
      path.extname(fName).toLowerCase() === ".md" ||
      path.extname(fName).toLowerCase() === ".mdx"
    ) {
      createTree(fName, false);
      await createJson(path.join(cPath, fName));
    }
  }))
}
await rc(contentPath)
const res = Object.freeze(ContentMap)

export default res
